﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.OrderFreightAppendRecord;
using BCDto.TMS.OrderFreightAppendRecord;
using BCEntity.TMS.OrderFreightAppendRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.TMS.OrderFreightAppendRecord
{
	public class OrderFreightAppendRecordService : IOrderFreightAppendRecordService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IOrderFreightAppendRecordData orderFreightAppendRecordData;
		private readonly string key = string.Format("{0}", EntityNameConst.OrderFreightAppendRecordEntity);

		public OrderFreightAppendRecordService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IOrderFreightAppendRecordData orderFreightAppendRecordData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.orderFreightAppendRecordData = orderFreightAppendRecordData;
		}

		#region 检查订单运费追加记录是否存在
		public bool Exists(long orderFreightAppendRecordId)
		{
			return this.orderFreightAppendRecordData.Exists(orderFreightAppendRecordId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加订单运费追加记录
		public OrderFreightAppendRecordDto Add(OrderFreightAppendRecordRequestDto requestDto)
		{
			var entity = requestDto.As<OrderFreightAppendRecordEntity>();

			return this.orderFreightAppendRecordData.Add(entity).GetAwaiter().GetResult().As<OrderFreightAppendRecordDto>();
		}
		#endregion

		#region 修改订单运费追加记录
		public OrderFreightAppendRecordDto Update(OrderFreightAppendRecordPutDto putDto)
		{
			if (!this.Exists(putDto.OrderFreightAppendRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<OrderFreightAppendRecordEntity>();
			var result = this.orderFreightAppendRecordData.Update(entity).GetAwaiter().GetResult().As<OrderFreightAppendRecordDto>();
			string cacheKey = this.redisService.GetKey(key, entity.OrderFreightAppendRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除订单运费追加记录
		public bool Delete(long orderFreightAppendRecordId)
		{
			if (!this.Exists(orderFreightAppendRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.orderFreightAppendRecordData.Delete(orderFreightAppendRecordId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, orderFreightAppendRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取订单运费追加记录
		public OrderFreightAppendRecordDto Get(long orderFreightAppendRecordId)
		{
			string cacheKey = this.redisService.GetKey(key, orderFreightAppendRecordId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderFreightAppendRecordData.GetEntity(orderFreightAppendRecordId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<OrderFreightAppendRecordDto>();
		}
		#endregion

		#region 获取订单运费追加记录列表
		public IEnumerable<OrderFreightAppendRecordDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.orderFreightAppendRecordData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderFreightAppendRecordDto>>();
		}
		#endregion

		#region 获取订单运费追加记录分页
		public PageableList<OrderFreightAppendRecordDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderFreightAppendRecordData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<OrderFreightAppendRecordDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<OrderFreightAppendRecordDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}